package edu.ntu.cloudroid.model.policy;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import edu.ntu.cloudroid.model.IDs;
import edu.ntu.cloudroid.model.UserData;
import edu.ntu.cloudroid.xacml.builder.policy.AbstractPolicyBuilder;
import edu.ntu.cloudroid.xacml.builder.policy.SimpleSimilarityBuilder;
import edu.ntu.cloudroid.xacml.builder.policy.SimpleSlidingWindowBuilder;
import edu.ntu.cloudroid.xacml.builder.policy.SimpleStatBuilder;
import edu.ntu.cloudroid.xacml.obligation.ObligationHandle;
import edu.ntu.cloudroid.xacml.obligation.SimilarityObligation;
import edu.ntu.cloudroid.xacml.obligation.SlidingWindowObligation;
import edu.ntu.cloudroid.xacml.obligation.StatsObligation;

//Simple similarity. Contains the function ID
@PersistenceCapable(detachable = "true")
@Inheritance
public class SimpleSimilarity extends AbstractPrivacyPreference {

	@Persistent
	private int similarityFunctionId;
	@Persistent
	private int granularity;
	@Persistent
	private double range;

	@Override
	public void initializePreference(String[] values) throws Exception {
		// functionId dataColumnIdx
		this.similarityFunctionId = new Integer(values[0]).intValue();
		this.setDataColumnIndex(new Integer(values[1]).intValue());
		this.granularity = new Integer(values[2]).intValue();
		this.range = new Double(values[3]).doubleValue();
		this.latestDataOnly = new Boolean(values[4]).booleanValue();
	}

	public int getSimilarityFuncId() {
		return this.similarityFunctionId;
	}

	public int getGranularity() {
		return granularity;
	}

	public double getRange() {
		return range;
	}

	@Override
	public AbstractPolicyBuilder getPolicyBuilder() {
		return new SimpleSimilarityBuilder();
	}


	@Override
	public UserData getMetaData(Object param) throws Exception {
		String type = "";
		if (param == null)
			type = type + AbstractPrivacyPreference.ISALL;
		else
			type = type + AbstractPrivacyPreference.ISGROUP + ","
					+ param.toString();

		String s = this.getUserName() + ","
				+ IDs.PolicyID.SimpleSimilarity.ordinal() + "," + type + ","
				+ this.similarityFunctionId + "," + this.getDataColumnIndex()
				+ "," + this.granularity + "," + this.range + ","
				+ this.isLatestDataOnly();

		return new PolicyInfo(this.getUserName(), s.split(","));
	}
}
